﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.CheckClass;
using ECCSystem.BizRule;
using System.Diagnostics;
using System.Net.NetworkInformation;
using ECCSystem.UserControls;
namespace ECCSystem
{
	public partial class frmEmailAppoint : Form
	{
		string strUserName = "";
		string strUserID = "";
		string strUserIP = "";
		int intMode = 0;
		public frmEmailAppoint()
		{
			InitializeComponent();
		}

		private void frmEmailAppoint_Load(object sender, EventArgs e)
		{
			BindData();
		}

		private bool Check()
		{
			DateTime dtStart = new DateTime();
			DateTime dtEnd = new DateTime();

			dtStart = dtpStart.Value;
			dtEnd = dtpEnd.Value;

			if (dtStart >= dtEnd)
			{
				MessageBox.Show("終了日付は開始日付より遅い！", "ECC Information");
				return false;
			}

			if (intMode != 0)
			{
				if (cmbMailCategory.Text.Trim() == "")
				{
					MessageBox.Show("カテゴリは空白不可!", "ECC Information");
					return false;
				}
			}

			//if (cmbMailCategory.Text.Trim() == "")
			//{
			//    MessageBox.Show("カテゴリ不可为空!", "ECC Information");
			//    return false;
			//}

			//if (cmbKeyName.Text.Trim() == "")
			//{
			//    MessageBox.Show("件名不可为空!", "ECC Information");
			//    return false;
			//}

			if (txtEmailAppointUser.Text.Trim() == "")
			{
				MessageBox.Show("ユーザは選択されていません", "ECC Information");
				return false;
			}

			if (intMode == 0)
			{
				if (strUserID.Trim() == "")
				{
					MessageBox.Show("ユーザは選択されていません！", "ECC Information");
					return false;
				}
			}
			return true;
		}

		#region 
		//private void btnAdd_Click(object sender, EventArgs e)
		//{
		//    //for (int i = 0; i < objAL.Count; i++)
		//    //{
		//    //    string strNetSend = "net send " + objAL[i].ToString() + 
		//    //                        " you will OverTime from " + 
		//    //                        dtpStart.Value.ToLongTimeString() + " to " + dtpEnd.Value.ToLongTimeString();

		//    //    Process process = new Process();//创建进程对象
		//    //    ProcessStartInfo startinfo = new ProcessStartInfo();//创建进程时使用的一组值，如下面的属性
		//    //    startinfo.FileName = "cmd.exe";//设定需要执行的命令程序... //以下是隐藏cmd窗口的方法
		//    //    startinfo.Arguments = "/c" + strNetSend;//设定参数，要输入到命令程序的字符，其中“/c"表示执行完命令后马上退出
		//    //    startinfo.UseShellExecute = false;//不使用系统外壳程序启动
		//    //    startinfo.RedirectStandardInput = false;//不重定向输入
		//    //    startinfo.RedirectStandardOutput = true;//重定向输出，而不是默认的显示在dos控制台上
		//    //    startinfo.CreateNoWindow = true;//不创建窗口
		//    //    process.StartInfo = startinfo;
		//    //    try
		//    //    {
		//    //        if (process.Start() == false)//开始进程
		//    //        {
		//    //        }
		//    //        else
		//    //        {
		//    //            objAL.RemoveAt(i);
		//    //            objALName.RemoveAt(i);
		//    //        }
		//    //    }
		//    //    catch (Exception ex)
		//    //    {

		//    //    }
		//    //    finally
		//    //    {
		//    //        if (process != null)
		//    //        {
		//    //            process.Close();
		//    //        }
		//    //    }

		//    //}
		//    //string str = "";
		//    //for (int j = 0; j < objALName.Count; j++)
		//    //{
		//    //    str = str + objALName[j].ToString() + ";";
		//    //}

		//    //if (str.Length > 0)
		//    //{
		//    //    MessageBox.Show("用户" + str + " 消息发送失败", "ECC Information");
		//    //}

		//    BindData();
		//}
		#endregion

		private void BindData()
		{
			ClsEmailAppoint obj = new ClsEmailAppoint();
			DataSet dt = (DataSet)obj.GetList();

			if (dt.Tables.Count == 0)
			{
				return;
			}
			dtvUser.AutoGenerateColumns = false;
			dtvUser.Sort(dtvUser.Columns["UserName"], ListSortDirection.Descending);
			dtvUser.DataSource = dt.Tables[0];

			ECCSystem.BizRule.ClsMailCategory objClsMailCategory = new ECCSystem.BizRule.ClsMailCategory();
			DataSet objDS1 = (DataSet)objClsMailCategory.GetList();
			//Don't show blank row

			if (objDS1.Tables.Count > 0)
			{
				DataTable dtTmp = objDS1.Tables[0];
				DataRow dr = dtTmp.NewRow();
				dr[0] = System.DBNull.Value;
				dr[1] = " ";
				dtTmp.Rows.InsertAt(dr, 0);
				
				cmbMailCategory.ValueMember = "CategoryScore_KeyID";
				cmbMailCategory.DisplayMember = "Category_Name";
				cmbMailCategory.DataSource = dtTmp;
			}

			if (dt.Tables.Count > 0)
			{
				this.btnDelete.Enabled = true;
				this.btnUpdata.Enabled = true;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
				if (dtvUser.Rows.Count > 0)
				{
					dtpStart.Value = Convert.ToDateTime(dtvUser.Rows[0].Cells["EmailAppoint_StarTime"].Value);
					dtpEnd.Value = Convert.ToDateTime(dtvUser.Rows[0].Cells["EmailAppoint_EndTime"].Value);
					txtEmailAppointUser.Text = Convert.ToString(dtvUser.Rows[0].Cells["UserName"].Value);
					txtEmailAppointUser.Tag = Convert.ToString(dtvUser.Rows[0].Cells["ID"].Value);
					cmbMailCategory.SelectedValue = Convert.ToString(dtvUser.Rows[0].Cells["CategoryScore_KeyID"].Value);
					cmbKeyName.SelectedValue = Convert.ToString(dtvUser.Rows[0].Cells["CategoryContent_KeyID"].Value);
				}
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdata.Enabled = false;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}
		}

		private void frmEmailAppoint_FormClosed(object sender, FormClosedEventArgs e)
		{
			Form frmMain = new frmMain();
			frmMain.Show();
		}

		private void btnAddUser_Click(object sender, EventArgs e)
		{
			DigUser objForm = new DigUser();
			if (txtEmailAppointUser.Text != "")
			{
				objForm.PersonID = Convert.ToString(txtEmailAppointUser.Tag);
				objForm.PersonName = txtEmailAppointUser.Text;
			}

			objForm.ShowDialog(this);
			strUserName = objForm.PersonName;
			strUserID = objForm.PersonID;
			strUserIP = objForm.PersonIP;
			this.txtEmailAppointUser.Text = strUserName;
			txtEmailAppointUser.Tag = strUserID;
		}

		private void btnSave_Click(object sender, EventArgs e)
		{

			ClsEmailAppoint obj = new ClsEmailAppoint();
			//string strUser = "";

			if (Check() == false)
			{
				return;
			}

			if (intMode == 0)
			{
				obj.AddEmailAppoint(dtpStart.Value, dtpEnd.Value, strUserName,
					strUserID, ClsCommon.gIntOperatorID, CheckClass.clsUICheck.NullToString(cmbKeyName.SelectedValue).Trim(), 
					CheckClass.clsUICheck.NullToString(cmbMailCategory.SelectedValue.ToString()));
			}
			else
			{
				obj.UpdateEmailAppoint(Convert.ToString(dtvUser.SelectedRows[0].Cells["KeyID"].Value),
										  dtpStart.Value,dtpEnd.Value,txtEmailAppointUser.Text.Trim(),
										  txtEmailAppointUser.Tag.ToString().Trim(), ClsCommon.gIntOperatorID,
										  CheckClass.clsUICheck.NullToString(cmbKeyName.SelectedValue).Trim(),
										  CheckClass.clsUICheck.NullToString(cmbMailCategory.SelectedValue.ToString()));
				intMode = 0;
			}

			this.btnDelete.Enabled = true;
			this.btnUpdata.Enabled = true;
			this.dtvUser.Enabled = true;
			this.txtEmailAppointUser.Text = "";
			cmbMailCategory.Text = "";
			this.BindData();
			ResetDateTime();
			
			
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			//wether have selected row,if have not selected row, pop-up prompt
			if (dtvUser.CurrentRow == null)
			{
				MessageBox.Show("データを選んでください！", "ECC Information");
				return;
			}
			else
			{
				if (MessageBox.Show("削除します、よろしいですか？", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
				{
					for (int i = 0; i < dtvUser.SelectedRows.Count; i++)
					{
						ECCSystem.BizRule.ClsEmailAppoint objBRMC = new ECCSystem.BizRule.ClsEmailAppoint();
						objBRMC.DeleteEmailAppoint(Convert.ToString(dtvUser.SelectedRows[i].Cells["KeyID"].Value));
					}
					this.BindData();

					dtpStart.Value = DateTime.Now;
					dtpEnd.Value = DateTime.Now;
					txtEmailAppointUser.Text = "";
					txtEmailAppointUser.Tag = "";
					cmbMailCategory.Text = "";
					cmbMailCategory.SelectedValue = System.DBNull.Value;
					cmbKeyName.SelectedValue = System.DBNull.Value;
				}

			}
			ResetDateTime();
		}

		private void btnUpdata_Click(object sender, EventArgs e)
		{
			if (dtvUser.CurrentRow == null)
			{
				MessageBox.Show("データを選んでください！", "ECC Information");
				return;
			}

			//dtpStart.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["EmailAppoint_StarTime"].Value);
			//dtpEnd.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["EmailAppoint_EndTime"].Value);
			//txtEmailAppointUser.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["UserName"].Value);
			//txtEmailAppointUser.Tag = Convert.ToString(dtvUser.SelectedRows[0].Cells["ID"].Value);
			//cmbMailCategory.SelectedValue = Convert.ToString(dtvUser.SelectedRows[0].Cells["CategoryScore_KeyID"].Value);
			//cmbKeyName.SelectedValue = Convert.ToString(dtvUser.SelectedRows[0].Cells["CategoryContent_KeyID"].Value);


			this.btnDelete.Enabled = false;
			this.btnUpdata.Enabled = false;
			this.dtvUser.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;
			intMode = 1;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			dtpStart.Value = DateTime.Now;
			dtpEnd.Value = DateTime.Now;
			txtEmailAppointUser.Text = "";
			txtEmailAppointUser.Tag = "";
			cmbMailCategory.Text = "";
			cmbMailCategory.SelectedValue = System.DBNull.Value;
			cmbKeyName.SelectedValue = System.DBNull.Value;
			ResetDateTime();
			intMode = 0;

			if (dtvUser.Rows.Count > 0)
			{
				this.btnDelete.Enabled = true;
				this.btnUpdata.Enabled = true;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;

				dtvUser.Rows[0].Selected = true;

				dtpStart.Value = Convert.ToDateTime(dtvUser.CurrentRow.Cells["EmailAppoint_StarTime"].Value);
				dtpEnd.Value = Convert.ToDateTime(dtvUser.CurrentRow.Cells["EmailAppoint_EndTime"].Value);
				txtEmailAppointUser.Text = Convert.ToString(dtvUser.CurrentRow.Cells["UserName"].Value);
				txtEmailAppointUser.Tag = Convert.ToString(dtvUser.CurrentRow.Cells["ID"].Value);
				cmbMailCategory.SelectedValue = Convert.ToString(dtvUser.CurrentRow.Cells["CategoryScore_KeyID"].Value);
				cmbKeyName.SelectedValue = Convert.ToString(dtvUser.CurrentRow.Cells["CategoryContent_KeyID"].Value);
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdata.Enabled = false;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}
		}

		private void ResetDateTime()
		{
			dtpStart.Value = DateTime.Now;
			dtpEnd.Value = DateTime.Now;
		}

		private void cmbMailCategory_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (cmbMailCategory.Text.Trim() != "")
			{
				ECCSystem.BizRule.ClsMailKeyName objClsMailKeyName = new ECCSystem.BizRule.ClsMailKeyName();
				DataSet objDS1 = (DataSet)objClsMailKeyName.GetListByID(cmbMailCategory.SelectedValue.ToString());
				//Don't show blank row

				if (objDS1.Tables.Count > 0)
				{
					DataTable dt = objDS1.Tables[0];
					DataRow dr = dt.NewRow();
					dr["CategoryContent_KeyID"] = System.DBNull.Value;
					dr["CategoryContent_Name"] = " ";
					dt.Rows.InsertAt(dr, 0);
					cmbKeyName.DataSource = dt;
					cmbKeyName.ValueMember = "CategoryContent_KeyID";
					cmbKeyName.DisplayMember = "CategoryContent_Name";
				}
			}
			else
			{
				cmbKeyName.DataSource = null;
			}

		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			dtpStart.Value = DateTime.Now;
			dtpEnd.Value = DateTime.Now;
			cmbMailCategory.Text = "";
			txtEmailAppointUser.Text = "";
			txtEmailAppointUser.Tag = "";

			this.btnDelete.Enabled = false;
			this.btnUpdata.Enabled = false;
			this.dtvUser.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;

			intMode = 0;
		}

		private void dtvUser_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			dtpStart.Value = DateTime.Now;
			dtpEnd.Value = DateTime.Now;
			txtEmailAppointUser.Text = "";
			txtEmailAppointUser.Tag = "";
			cmbMailCategory.Text = "";
			cmbMailCategory.SelectedValue = System.DBNull.Value;
			cmbKeyName.SelectedValue = System.DBNull.Value;

			if (dtvUser.SelectedRows.Count <= 0)
			{
				return;
			}
			dtpStart.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["EmailAppoint_StarTime"].Value);
			dtpEnd.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["EmailAppoint_EndTime"].Value);
			txtEmailAppointUser.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["UserName"].Value);
			txtEmailAppointUser.Tag = Convert.ToString(dtvUser.SelectedRows[0].Cells["ID"].Value);
			cmbMailCategory.SelectedValue = Convert.ToString(dtvUser.SelectedRows[0].Cells["CategoryScore_KeyID"].Value);
			cmbKeyName.SelectedValue = Convert.ToString(dtvUser.SelectedRows[0].Cells["CategoryContent_KeyID"].Value);
		}


	}
}